﻿// ***********************************************************************
// Assembly         : MVC4Portal
// Author           : LJO+01 (Laurent Jordi)
// Created          : 06-05-2013
// Last Modified By : LJO+01 (Laurent Jordi)
// Last Modified On : 06-05-2013
// ***********************************************************************
// <copyright file="FilesController.cs" company="Copyright © Innovacall 2011">
//     Copyright (c) Copyright © Innovacall 2011. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElFinder;
using InnovaApp.Layers.ResourcesLayer.Properties;
using INNOVACALL.Framework.Common.Extensions;
using INNOVACALL.Framework.Common.MVCHelpers;

namespace InnovaApp.Portals.MVC4Portal.Controllers.FilesManagement
{
    /// <summary>
    /// Standard controller for ELFinder
    /// </summary>
    public class ELFinderConnectorController : INNOVACALL.Framework.Common.Controllers.ViewControllers._ViewControllers_Base
    {

        /// <summary>
        /// Gets the upload path.
        /// </summary>
        /// <value>The upload path.</value>
        protected string UploadPath
        {
            get
            {
                string toReturn = PortalEnvironment.UploadPath; ;
                return toReturn;
            }
        }

        /// <summary>
        /// Gets the upload path slash.
        /// </summary>
        /// <value>The upload path slash.</value>
        protected string UploadPathSlash
        {
            get
            {
                string toReturn;
                toReturn = (UploadPath.Trim().EndsWith("/") ? UploadPath.Trim() : string.Format("{0}/", UploadPath.Trim()));
                return toReturn;
            }
        }
        /// <summary>
        /// Indexes this instance.
        /// </summary>
        /// <returns>ActionResult.</returns>
        [Authorize, CacheManager(enCacheMode.NoCache)]
        public ActionResult Index()
        {
            FileSystemDriver driver = new FileSystemDriver();
            driver.AddRoot(new Root(new DirectoryInfo(Server.MapPath(UploadPath)), UploadPathSlash) { IsReadOnly = false, Alias = Resources.View_Finder_RootFolder_Title });
            var connector = new Connector(driver);
            return connector.Process(this.HttpContext.Request);
        }

        /// <summary>
        /// Selects the file.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <returns>ActionResult.</returns>
        [HttpPost, Authorize, CacheManager(enCacheMode.NoCache)]
        public ActionResult SelectFile(string target)
        {
            string toReturn = string.Empty;
            FileSystemDriver driver = new FileSystemDriver();
            driver.AddRoot(new Root(new DirectoryInfo(Server.MapPath(UploadPath)), UploadPathSlash) { IsReadOnly = false, Alias = Resources.View_Finder_RootFolder_Title });
            var connector = new Connector(driver);
            string localFileName = connector.GetFileByHash(target).FullName;
            string localUploadedContentRootPath = Server.MapPath(UploadPath);
            string relativeFilePath = localFileName.Replace(localUploadedContentRootPath, string.Empty);
            string fileUrl = string.Format("{0}{1}", UploadPath, relativeFilePath.Replace(@"\", @"/"));
            toReturn = fileUrl;
            return Json(toReturn);
        }
    }
}
